package com.moody.init;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.moody.hibernate.dao.ReportItemDAO;
import com.moody.hibernate.domain.ReportItem;

/*
 * Copyright © Moody analytics
 *
 * @Author: Wang Fuqiang
 * @Time: 2011-9-14 下午03:42:54
 * @Modified by: Wangfuqiang 2011-09-07
 * @Description: 
 */
public class ReportItemInit {
	private static final Logger log = LoggerFactory.getLogger(DataDictionary.class);
	private static ReportItemInit instance = null;
	static WebApplicationContext web = ContextLoader.getCurrentWebApplicationContext();
	static ServletContext context = web.getServletContext();  
	static WebApplicationContext ctx  = WebApplicationContextUtils.getWebApplicationContext(context);
	private static ReportItemDAO reportItemDao = ReportItemDAO.getFromApplicationContext(ctx);
	private static Map<String, String> map = new LinkedHashMap<String, String>();
	
	private ReportItemInit(){
		
	}
	
	public static ReportItemInit getInstance(){
		if(instance == null){
			instance = new ReportItemInit();
		}
		return instance;
	}
	
	public Map<String,String> getReportItem(){
		if(map.size() > 0 )
			return map;
		else{
			List<ReportItem> list  = reportItemDao.findAll();
		
			if(list.size() <=0 ){
				log.error("list size less than 0");
			}else{
				for(ReportItem reportItem : list ){
					map.put(reportItem.getReportItemNo(), reportItem.getReportItemName());
				}
			}
		}
		return map;
	}
}
